package vn.smartoffice.mybatis.plugin;

import java.sql.CallableStatement;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedTypes;
import org.apache.ibatis.type.TypeHandler;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalDate;

@MappedTypes(LocalDate.class)
public class LocalDateTypeHandler implements TypeHandler<LocalDate> {

	@Override
	public void setParameter(PreparedStatement ps, int i, LocalDate parameter,
			JdbcType jdbcType) throws SQLException {
		LocalDate date = (LocalDate) parameter;
		if (date != null) {
			ps.setDate(i, new Date(date.toDateTimeAtStartOfDay().toDate()
					.getTime()));
		} else {
			ps.setDate(i, null);
		}
	}

	@Override
	public LocalDate getResult(ResultSet rs, String columnName)
			throws SQLException {
		Date date = rs.getDate(columnName);
		if (date != null) {
			return new LocalDate(date.getTime(), DateTimeZone.UTC);
		} else {
			return null;
		}
	}

	@Override
	public LocalDate getResult(CallableStatement cs, int columnIndex)
			throws SQLException {
		Date date = cs.getDate(columnIndex);
		if (date != null) {
			return new LocalDate(date.getTime(), DateTimeZone.UTC);
		} else {
			return null;
		}
	}

	@Override
	public LocalDate getResult(ResultSet arg0, int arg1) throws SQLException {
		Date date = arg0.getDate(arg1);
		if (date != null) {
			return new LocalDate(date.getTime(), DateTimeZone.UTC);
		} else {
			return null;
		}
	}

}
